package com.weina.service.impl;

import com.weina.dao.EmployeeMapper;
import com.weina.entity.Employee;
import com.weina.service.EmployeeService;
import com.weina.utils.PasswordUtil;
import com.weina.utils.SystemConstant;
import com.weina.utils.UUIDUtils;
import com.weina.vo.EmployeeVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {
    @Resource
    private EmployeeMapper employeeMapper;

    @Override
    public Employee login(String loginName, String loginPwd) {
        Employee employee = employeeMapper.findEmployeeByLoginName(loginName);
        if(employee!=null){
            String newPassword=PasswordUtil.md5(loginPwd,employee.getSalt(), SystemConstant.PASSWORD_COUNT);
            if(employee.getLoginpwd().equals(newPassword)){
                //登录成
                return employee;
            }
        }
        //登录失败
        return null;
    }

    @Override
    public Integer getEmployeeCountByRoleId(Integer id) {
        return employeeMapper.getEmployeeCountByRoleId(id);
    }

    @Override
    public List<Employee> getAllEmployee(EmployeeVo employeeVo) {
        return employeeMapper.findAllEmployee(employeeVo);
    }

    @Override
    public int addEmployee(Employee employee) {
        employee.setSalt(UUIDUtils.randromUUID());//设置加密盐值
        employee.setCreateDate(new Date());//创建时间
        employee.setLoginpwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,employee.getSalt(),SystemConstant.PASSWORD_COUNT));//密码
        return employeeMapper.addEmployee(employee);
    }

    @Override
    public int updateEmploye(Employee employee) {
        employee.setModifyDate(new Date());
        return employeeMapper.updateEmployee(employee);
    }

    @Override
    public int deleteEmployeeById(Integer id) {
        //删除员工角色中间表的 绑定记录
        employeeMapper.deleteEmployeeAndRole(id);
        //删除员工，
        return employeeMapper.deleteEmployee(id);
    }

    @Override
    public int resetPwd(int id) {
        Employee employee = new Employee();
        employee.setSalt(UUIDUtils.randromUUID());
        employee.setLoginpwd(PasswordUtil.md5(SystemConstant.DEFAULT_LOGIN_PWD,employee.getSalt(),SystemConstant.PASSWORD_COUNT));
        employee.setId(id);
        return employeeMapper.updateEmployee(employee);
    }

    @Override
    public boolean saveEmployeeRole(String roleIds, Integer empId) {
        try {
            //1.删除该用户原来的所有角色
            employeeMapper.deleteEmployeeAndRole(empId);
            String [] idArr=roleIds.split(",");
            for(int i=0;i<idArr.length;i++){
                employeeMapper.addEmployeeRole(idArr[i],empId);
            }
            //2.循环插入该用户选择的新角色列表
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

}
